<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.aut_name"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate|relativeTime}}</span>
            <van-button @click="clickShowReply(item)" size="mini" type="default">回复</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" name="like-o" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入评论内容"
        v-model="content"
      >
        <van-button @click="addComment" slot="button" size="mini" type="info">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
    <!-- 弹出层 -->
<van-popup v-model="isShowReply" round position="bottom" :style="{ height: '75%' }">
  <comment-reply v-if="isShowReply" :comment='currentComment'></comment-reply>
</van-popup>
  </div>
</template>

<script>
import { reqGetComments, reqAddComment } from '@/api/article'
import commentReply from './commentReply'
export default {
  name: 'ArticleComment',
  components: { commentReply },
  props: {
    articleid: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      content: '', // 评论内容
      isShowReply: false,
      currentComment: {}// 表示当前的回复组件, 回复的是哪个评论
    }
  },

  methods: {
    async onLoad () {
      const res = await reqGetComments(this.articleid, this.offset)
      //   console.log(res)
      const data = res.data.data.results
      console.log(data)
      // 1 保存数据
      this.list.push(...data)
      // 2 关闭loading
      this.loading = false
      // 设置offset
      this.offset = res.data.data.results
      if (!data.length) {
        this.finished = true
      }
    },
    async addComment () {
      if (this.content === '') return
      console.log('aaa')
      const res = await reqAddComment(this.articleid, this.content)
      console.log(res)
      // 从接口的返回值中, 会有一个new_obj数据, 就表示新发布的评论对象
      this.list.unshift(res.data.data.new_obj)
      // 评论输入框清空
      this.content = ''
      this.$toast.success('评论成功')
    },
    clickShowReply (item) {
      // 1. 展示回复组件
      this.isShowReply = true
      // 2.更新正在回复的组件
      console.log(item)
      this.currentComment = item
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
